from rest_framework.serializers import ModelSerializer
from rest_framework import serializers

from company.models import VehicleCompanyTerminal
from terminal.models import Terminal, TerminalControl, TerminalParams


class TerminalSerializer(ModelSerializer):
    class Meta:
        model = Terminal
        fields = (
            'id', 'province', 'city', 'imei',
            'uid', 'iccid', 'device_type', 'custom_status', 'status', 'firmware_version',
            'created_time', 'modified_on',
        )


class TerminalCreateSerializer(ModelSerializer):
    class Meta:
        model = Terminal
        fields = '__all__'


class TerminalParamsSerializer(ModelSerializer):
    class Meta:
        model = TerminalParams
        fields = '__all__'


class TerminalControlSerializer(ModelSerializer):
    class Meta:
        model = TerminalControl
        fields = '__all__'


class TerminalListSerializer(ModelSerializer):

    class Meta:
        model = Terminal
        fields = ['id', 'hardware_version', 'imei', 'firmware_version', 'device_type', 'custom_status', 'created_time']

    def to_representation(self, instance):
        return {
            'id': instance.id,
            'hardware_version': instance.hardware_version,
            'imei': instance.imei,
            'firmware_version': instance.firmware_version,
            'device_type': instance.get_device_type_display(),
            'custom_status': instance.get_custom_status_display(),
            'created_time': instance.created_time.strftime('%Y-%m-%d %H:%M:%S')
        }
